class TrieNode:
def __init__(self, Node = None, flag = False):
self.letters = [None] * 27
self.flag = flag
if Node:
self.letters[ord(letter) - 97] = Node
class Trie:
def __init__(self):
self.root = TrieNode()
def insert(self, word: str) -> None:
a = self.root
for i in range(len(word)):
index = ord(word[i]) - 97
if not a.letters[index]:
if i == len(word) -1:
a.letters[index] = TrieNode(flag = True)
a = a.letters[index]
continue
a.letters[index] = TrieNode()
a = a.letters[index]
else:
a = a.letters[index]
if i == len(word) -1:
a.flag = True
continue
def search(self, word: str) -> bool:
a = self.root
index = ord(word[i]) - 97
for i in range(len(word)):
if not a.letters[index]:
return False
else:
a = a.letters[index]
if i == len(word) -1:
if a.flag == True:
return True
continue
return False
def startsWith(self, prefix: str) -> bool:
a = self.root
for i in range(len(prefix)):
if not a.letters[ord(prefix[i]) - 97]:
return False
else:
a = a.letters[ord(prefix[i]) - 97]
return True
# Your Trie object will be instantiated and called as such:
# obj = Trie()
# obj.insert(word)
# param_2 = obj.search(word)
# param_3 = obj.startsWith(prefix)
Factorial equations | Removal of vertices |
Happy segments | Cyclic shifts |
Zoos | Build a graph |
Almost correct bracket sequence | Count of integers |
Differences of the permutations | Doctor's Secret |
Back to School | I am Easy |
Teddy and Tweety | Partitioning binary strings |
Special sets | Smallest chosen word |
Going to office | Color the boxes |
Missing numbers | Maximum sum |
13 Reasons Why | Friend's Relationship |
Health of a person | Divisibility |
A. Movement | Numbers in a matrix |
Sequences | Split houses |
Divisible | Three primes |